###############################################
# Dynamic DiD: All Visas and Crime Concerns
###############################################

rm(list=ls())

library(Hmisc)
library(readstata13)
library(foreign)
library(tidyverse)
library(did)
library(ggplot2)
set.seed(1111)

#sink("01_results_concerns_all.txt")

############################
# Read and prepare data 
############################

# load data
load("final_county_2023april6.RData")
names(d_county)

#######################
# Results 
#######################

# d-did
out1 <- att_gt(yname = "crime_concerns_s",
               gname = "first.treat_all",
               idname = "county",
               tname = "survey",
               data = d_county,
)

es1 <- aggte(out1, type = "dynamic",na.rm = TRUE)
summary(es1)
ggdid(es1)

# save results for overall effect (Table A17)
pe1 <- es1$overall.att
se1 <- es1$overall.se
results1 <- rbind(pe1,se1) 
results1
write.table(results1, "overallATT_1.txt", sep="\t")

# table formal to prepare plot
event_study_diff_never <-   data.frame(
  type          = "dynamic",
  term = paste0('ATT(', es1$egt, ")"),
  event.time= es1$egt,
  estimate  = es1$att.egt,
  std.error = es1$se.egt,
  conf.low  = es1$att.egt - es1$crit.val.egt * es1$se.egt,
  conf.high = es1$att.egt + es1$crit.val.egt  * es1$se.egt)

# plot
p_es_never1 <- ggplot(data = event_study_diff_never,
                     mapping = aes(x = event.time, y = estimate)) +
  geom_line(size = 0.5, alpha = 2, colour = "black") +
  theme_light()  +
  geom_vline(xintercept = 0-0.05, color = 'grey', size = 1.2, linetype = "dotted") + 
  geom_hline(yintercept = 0, colour="black",  linetype = "dotted")+
  geom_pointrange(aes(ymin = conf.low, ymax = conf.high), show.legend = FALSE, linetype= 1, size = 1.1,
                  color=ifelse(event_study_diff_never$event.time<0,"gray60","black"))+
  xlab("Survey wave since immigration shock") +
  ylab("DiD Estimate (in standard deviation units)") +
  scale_x_continuous(breaks = c(-5:3)) +
  scale_y_continuous(limits = c(-6.5,6.5)) +
  theme(axis.text.y = element_text(size = 14),axis.title.y = element_text(size = 18)) +
  theme(axis.text.x = element_text(size = 14),axis.title.x = element_text(size = 18)) +
  theme(axis.title = element_text(color="black",  size = 12))+
  theme(plot.title=ggtext::element_markdown(size = 12,
                                            color="black",
                                            hjust=0,
                                            lineheight=1.2)
  )+
  annotate(geom="text", x=-0.5, y=5.75, label="Average of post-treatment coefficients (in standard deviation units): 
                                            -0.658 
                                            (0.308)",
           color="black",
           size = 7)

p_es_never1

# figure 1 all countries
tiff('figure1_all.tiff', units="in", width=10, height=7, res=200, compression = 'lzw')
p_es_never1
dev.off()

# table a13
sink(file = "tableA13.txt")
summary(es1)
sink(file = NULL)

sink()
